package com.rzgeo.rzmaplib.da;

import android.util.Log;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.ShapefileFeatureTable;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.Renderer;
import com.linuxense.javadbf.DBFDataType;
import com.linuxense.javadbf.DBFField;
import com.rzgeo.rzmaplib.util.IRmlLoadListener;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by zhanghang on 2018/1/19.
 */

public class RmlShpHolder {


    public interface OnPushFeature {
        void onInit(ArrayList<Feature> features);

        void onFinish(boolean success, String msg, int[] effectIds);
    }

    public interface OnEditFeature {
        void onFinish(boolean success, String msg, int[] effectIds);
    }


    private final FeatureLayer _layer;

    public RmlShpHolder(String path, MapView mapView, Renderer renderer, IRmlLoadListener onLoadListener) {
        // 构建ShapefileFeatureTable，引入本地存储的shapefile文件 构建FeatureLayer
        _layer = new FeatureLayer(
                new ShapefileFeatureTable(path));
        // 设置Shapefile文件的渲染方式
        _layer.setRenderer(renderer);
        mapView.getMap().getOperationalLayers().add(_layer);
    }

    public void pushFeatures(int num, final OnPushFeature onPushFeature) {
        if (num < 1)
            onPushFeature.onFinish(false, "创建要素个数不得小于1", null);

        ShapefileFeatureTable table = (ShapefileFeatureTable) _layer.getFeatureTable();

        if (table.isEditable() && table.canAdd()) {
            // 构建待增加的Feature对象，设置几何，设置属性
            ArrayList<Feature> features = new ArrayList<>(num);

            for (int i = 0; i < num; i++) {
                features.add(table.createFeature());
            }

            onPushFeature.onInit(features);
            // 调用addFeatureAsync方法增加要素
            final ListenableFuture<Void> addFeatureOper = table.addFeaturesAsync(features);

            // 在操作完成的监听事件中判断操作是否成功
            addFeatureOper.addDoneListener(new Runnable() {
                @Override
                public void run() {
                    try {
                        addFeatureOper.get();
                        if (addFeatureOper.isDone()) {
//                            addFeatureOper.get()
                            Log.i("ShapefileEdit:", "Feature added!");
                        } else {
                            onPushFeature.onFinish(
                                    false,
                                    "数据添加未能完成",
                                    null);
                        }
                    } catch (Exception e) {
                        onPushFeature.onFinish(
                                false,
                                "数据添加失败：" + e.getMessage(),
                                null);
                    }
                }
            });
        } else {
            onPushFeature.onFinish(
                    false,
                    "该数据不可编辑",
                    null);
        }
    }

    public void updateFeatures(ArrayList<Feature> features, OnEditFeature onFeature) {

        ShapefileFeatureTable table = (ShapefileFeatureTable) _layer.getFeatureTable();

        final ListenableFuture<Void> updateFeatureOper = table.updateFeaturesAsync(features);

        // 在操作完成的监听事件中判断操作是否成功
        updateFeatureOper.addDoneListener(new Runnable() {
            @Override
            public void run() {

            }
        });
    }

    public void deleteFeatures(ArrayList<Feature> features, OnEditFeature onFeature) {

        ShapefileFeatureTable table = (ShapefileFeatureTable) _layer.getFeatureTable();

        final ListenableFuture<Void> delFeatureOper = table.deleteFeaturesAsync(features);

        // 在操作完成的监听事件中判断操作是否成功
        delFeatureOper.addDoneListener(new Runnable() {
            @Override
            public void run() {

            }
        });
    }
}
